package com.wxyzz.satserver.user.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wxyzz.common.core.entity.User;
import com.wxyzz.common.core.utils.UserUtils;
import com.wxyzz.common.minio.utils.MinioUtil;
import com.wxyzz.satserver.user.dao.UserDao;
import com.wxyzz.satserver.user.param.UserParam;
import com.wxyzz.satserver.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * --- 代码敲烂 月薪过万 ---
 *
 * @author zhouhaoyu
 * @date 2024/11/14
 * @desc
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Resource
    private MinioUtil minioUtil;
    @Override
    public void addUser(User user) {

        User user1 = userDao.selectOne(new LambdaQueryWrapper<User>()
                        .eq(User::getPhone, user.getPhone())
                    .eq(User::getUsername,user.getUsername())
                .eq(User::getEmail,user.getEmail()));
        if (user1!= null){
            throw new RuntimeException("该用户已存在");
        }
        if (user.getPhone() == null || user.getPhone().isEmpty()){
            throw new RuntimeException("手机号不能为空");
        }
        if (user.getUsername() == null || user.getUsername().isEmpty()){
            throw new RuntimeException("用户名不能为空");
        }
        if (user.getPassword() == null || user.getPassword().isEmpty()){
            throw new RuntimeException("密码不能为空");
        }
        Pattern emailpattern = Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");
        // 判断邮箱格式
        if(!emailpattern.matcher(user.getEmail()).matches()){
            throw new RuntimeException("邮箱格式错误");
        }
        Pattern phonepattern = Pattern.compile("^1[3-9]\\d{9}$");
        // 判断手机号格式
        if (!phonepattern.matcher(user.getPhone()).matches()){
            throw new RuntimeException("手机号格式错误");
        }
        // md5加密密码
        String password = SecureUtil.md5(user.getPassword());
        user.setPassword(password);
        userDao.insert(user);
    }

    @Override
    public String uploadavatar(MultipartFile file) {
        // 获得原始文件名
        String filename = file.getOriginalFilename();
        int i = filename.lastIndexOf(".");
        filename = filename.substring(i);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + filename;

        String path = null;
        try {
            path = minioUtil.upload(file.getInputStream(),filename,file.getContentType());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return path;
    }

    @Override
    public String selectUserAvater(Integer uid) {
        User user = userDao.selectById(uid);
        return user.getAvatar();
    }

    @Override
    public User selectById(Integer id) {
        User user = userDao.selectById(id);
        return user;
    }

    @Override
    public void updateUser(UserParam user) {
        user.setId(UserUtils.getUid());
        userDao.updateByUserId(user);
    }

    @Override
    public UserParam findUserById() {
        Integer uid = UserUtils.getUid();
        User user = userDao.selectById(uid);
        UserParam userParam = new UserParam();
        BeanUtils.copyProperties(user,userParam);
        return userParam;
    }

    @Override
    public void updateBackground(MultipartFile file) {
        String filename = file.getOriginalFilename();
        int i = filename.lastIndexOf(".");
        filename = filename.substring(i);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + filename;

        String path = null;
        try {
            path = minioUtil.upload(file.getInputStream(),filename,file.getContentType());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        userDao.update(null,new LambdaUpdateWrapper<User>()
                        .set(User::getBackgroundImg,path)
                        .eq(User::getId,UserUtils.getUid())
                        );
    }
}
